package cpp.ece480.compteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SelectItem extends Activity {

	public String serverName;
	public String item;
	public String price;

	public int tableNumber;
	public int customerNumber;

	public boolean table1selected;
	public boolean table2selected;
	public boolean table3selected;
	public boolean table4selected;
	public boolean table5selected;
	public boolean table6selected;

	public boolean customer1selected;
	public boolean customer2selected;
	public boolean customer3selected;
	public boolean customer4selected;

	public MySQL_AndroidInterface mySQLInterface = new MySQL_AndroidInterface();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.items);

		table1selected = ((GlobalVariable) this.getApplication()).getTable1();
		table2selected = ((GlobalVariable) this.getApplication()).getTable2();
		table3selected = ((GlobalVariable) this.getApplication()).getTable3();
		table4selected = ((GlobalVariable) this.getApplication()).getTable4();
		table5selected = ((GlobalVariable) this.getApplication()).getTable5();
		table6selected = ((GlobalVariable) this.getApplication()).getTable6();

		if (table1selected) {
			tableNumber = 1;
		} else if (table2selected) {
			tableNumber = 2;
		} else if (table3selected) {
			tableNumber = 3;
		} else if (table4selected) {
			tableNumber = 4;
		} else if (table5selected) {
			tableNumber = 5;
		} else if (table6selected) {
			tableNumber = 6;
		} else {
			tableNumber = 0;
		}

		customer1selected = ((GlobalVariable) this.getApplication())
				.getCustomer1();
		customer2selected = ((GlobalVariable) this.getApplication())
				.getCustomer2();
		customer3selected = ((GlobalVariable) this.getApplication())
				.getCustomer3();
		customer4selected = ((GlobalVariable) this.getApplication())
				.getCustomer4();

		if (customer1selected) {
			customerNumber = 1;
		} else if (customer2selected) {
			customerNumber = 2;
		} else if (customer3selected) {
			customerNumber = 3;
		} else if (customer4selected) {
			customerNumber = 4;
		} else {
			customerNumber = 0;
		}

		Spinner spinnerBev = (Spinner) findViewById(R.id.spinnerBeverages);
		ArrayAdapter<CharSequence> adapterBev = ArrayAdapter
				.createFromResource(this, R.array.beverage_array,
						android.R.layout.simple_spinner_item);
		adapterBev
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerBev.setAdapter(adapterBev);

		Spinner spinnerBrkfst = (Spinner) findViewById(R.id.spinnerBreakfast);
		ArrayAdapter<CharSequence> adapterBrkfst = ArrayAdapter
				.createFromResource(this, R.array.breakfast_array,
						android.R.layout.simple_spinner_item);
		adapterBrkfst
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerBrkfst.setAdapter(adapterBrkfst);

		Spinner spinnerLunch = (Spinner) findViewById(R.id.spinnerLunch);
		ArrayAdapter<CharSequence> adapterLunch = ArrayAdapter
				.createFromResource(this, R.array.lunch_array,
						android.R.layout.simple_spinner_item);
		adapterLunch
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerLunch.setAdapter(adapterLunch);

		Spinner spinnerDin = (Spinner) findViewById(R.id.spinnerDinner);
		ArrayAdapter<CharSequence> adapterDin = ArrayAdapter
				.createFromResource(this, R.array.dinner_array,
						android.R.layout.simple_spinner_item);
		adapterDin
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerDin.setAdapter(adapterDin);

		Spinner spinnerDes = (Spinner) findViewById(R.id.spinnerDessert);
		ArrayAdapter<CharSequence> adapterDes = ArrayAdapter
				.createFromResource(this, R.array.dessert_array,
						android.R.layout.simple_spinner_item);
		adapterDes
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerDes.setAdapter(adapterDes);

		spinnerBev
				.setOnItemSelectedListener(new BeverageItemSelectedListener());
		spinnerBrkfst
				.setOnItemSelectedListener(new BreakfastItemSelectedListener());
		spinnerLunch.setOnItemSelectedListener(new LunchItemSelectedListener());
		spinnerDin.setOnItemSelectedListener(new DinnerItemSelectedListener());
		spinnerDes.setOnItemSelectedListener(new DessertItemSelectedListener());
	}

	public class BeverageItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			switch (pos) {
			case 0:
				break;
			case 1:
				item = "Cappuccino";
				break;
			case 2:
				item = "Juice";
				break;
			case 3:
				item = "Lemonade";
				break;
			case 4:
				item = "Milk";
				break;
			}
			if (pos != 0) {
				mySQLInterface.MySQL_UPDATE2("10.0.2.2", "tables", "ordernum",
						"beverages", item, "customerNum",
						String.valueOf(customerNumber), "tablenum",
						String.valueOf(tableNumber));
				launchCustomerScreen();
			}
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	public class BreakfastItemSelectedListener implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			switch (pos) {
			case 0:
				break;
			case 1:
				item = "Burrito";
				break;
			case 2:
				item = "Omelette";
				break;
			case 3:
				item = "Pancakes";
				break;
			}
			if (pos != 0) {
				mySQLInterface.MySQL_UPDATE2("10.0.2.2", "tables", "ordernum",
						"breakfast", item, "customerNum",
						String.valueOf(customerNumber), "tablenum",
						String.valueOf(tableNumber));
				launchCustomerScreen();
			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	public class LunchItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			switch (pos) {
			case 0:
				break;
			case 1:
				item = "Burger";
				break;
			case 2:
				item = "Salad";
				break;
			case 3:
				item = "Sandwich";
				break;
			}
			if (pos != 0) {
				mySQLInterface.MySQL_UPDATE2("10.0.2.2", "tables", "ordernum",
						"lunch", item, "customerNum",
						String.valueOf(customerNumber), "tablenum",
						String.valueOf(tableNumber));
				launchCustomerScreen();
			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	public class DinnerItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			switch (pos) {
			case 0:
				break;
			case 1:
				item = "Chicken";
				break;
			case 2:
				item = "Skillet";
				break;
			case 3:
				item = "Spaghetti";
				break;
			case 4:
				item = "Steak";
				break;
			}
			if (pos != 0) {
				mySQLInterface.MySQL_UPDATE2("10.0.2.2", "tables", "ordernum",
						"dinner", item, "customerNum",
						String.valueOf(customerNumber), "tablenum",
						String.valueOf(tableNumber));
				launchCustomerScreen();
			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	public class DessertItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			switch (pos) {
			case 0:
				break;
			case 1:
				item = "Cake";
				break;
			case 2:
				item = "Cheesecake";
				break;
			case 3:
				item = "Sundae";
				break;
			}
			if (pos != 0) {
				mySQLInterface.MySQL_UPDATE2("10.0.2.2", "tables", "ordernum",
						"dessert", item, "customerNum",
						String.valueOf(customerNumber), "tablenum",
						String.valueOf(tableNumber));
				launchCustomerScreen();
			}

			// Go back to customer order screen
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	public void launchCustomerScreen() {
		Intent intent = new Intent(this, CustomerOrders.class);
		startActivity(intent);
	}

}
